<template>
  <div class="fetch-password">
    <div class="fetch-password__title">
      邮箱找回密码
    </div>
    <div class="fetch-password__form">
      <el-form :model="fetchPassword" :rules="rules" ref="fetchPassword">
        <div class="fetchPassword-tip">
          请输入您的注册邮箱，我们会将重设密码的链接发到您的邮箱中。
        </div>
        <el-form-item prop="corp_id">
          <el-input type="text" v-model="fetchPassword.corp_id" placeholder="请输入企业ID"></el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input type="text" v-model="fetchPassword.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="submitForm" class="login-submit" >{{submiting ? '提交中...':'提交'}}</el-button>
        </el-form-item>
      </el-form>
      <div class="login-wrap">
        <router-link id="login-link" to="login" class="login">登陆</router-link>
      </div>
    </div>
  </div>
</template>
<script>
import api from '@/api'
export default {
  name: 'login',

  data () {
    return {
      submiting: false,
      fetchPassword: {
        email: '',
        corp_id: ''
      },
      rules: {
        corp_id: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm () {
      if (this.submiting) return
      this.$refs.fetchPassword.validate(async (valid) => {
        if (valid) {
          this.submiting = true
          try {
            await api.forgotPassword(this.fetchPassword)
            alert('邮件发送成功')
          } catch (err) {
            alert('请求失败' + err.response.data.error.msg)
          }
          this.submiting = false
        }
      })
    }
  }
}
</script>

<style lang="stylus" scoped>
.fetch-password 
  width 500px
  border 1px solid #eee
  border-radius 12px
  margin 200px auto 1000px
  padding-bottom 20px
  box-shadow: 0 3px 6px rgba(0,0,0,.15)
  &__title
    font-size: 20px;
    margin 30px 0 25px
  &__form
    box-sizing border-box
    padding 0 80px
    .fetchPassword-tip
      font-size 12px
      text-align left
      margin-bottom 12px
    .login-submit
      width 260px
      margin-top 5px
      border-radius 12px
    .login-wrap
      a
        color #a5a4a4
        text-decoration none
</style>

